var Bank = function() {

	var currentBankObj = null;
	var left_margin_step = 15;
	var bankTypeIdObj = null;

	// ���������𣬼�������
	$(".bank_category_trigger")
			.live(
					'click',
					function() {
						var trigger = $(this);
						if (trigger.hasClass("close")) {
							trigger.parent(".bank_tree_category").next(
									".bank_tree_items").slideUp(function() {
								trigger.removeClass("close");
							});
							return false;
						}

						trigger.addClass("close");

						var pkFather = "";
						var istype = trigger.next("input").val();
						if (istype == "Y") {
							pkFather = trigger.next().next("label").attr("id");
						} else {
							pkFather = trigger.next().next().next("label")
									.attr("id");
						}
						var formStr = $(this).parents("div").find("#bank_filter").serialize();
						var myts = new Date();
						jQuery.ajax( {
									type : "POST",
									async : false,
									url : ebvp_context
											+ "/register/qryBankDocListByFatherPk?pkFather="
											+ pkFather + "&istype=" + istype
											+ "&myts=" + myts,
									dataType : "text",
									data : "&" + formStr,
									success : function(result) {
										if (result != null && result != "[]") {
											var arr = eval(result);
											var treeItems = $("<div></div>")
													.addClass("bank_tree_items");
											var current_margin_left_size = parseInt(trigger
													.attr("margin"))
													+ left_margin_step;

											for ( var loop = 0; loop < arr.length; loop++) {
												var cate = $("<div></div>")
														.addClass(
																"bank_tree_category");
												var objTrigger = $("<a></a>")
														.attr(
																"style",
																"margin-left:"
																		+ current_margin_left_size
																		+ "px")
														.attr("margin",
																current_margin_left_size)
														.addClass(
																"bank_category_trigger");
												var isType = $(
														"<input></input>")
														.attr("name", "istype")
														.attr("type", "hidden")
														.val("N");
												var inputRadio = $(
														"<input></input>")
														.attr("type", "radio")
														.attr(
																"id",
																arr[loop].pk_bankdoc)
														.attr("name", "bankid");
												var labelBank = $(
														"<label></label>")
														.text(arr[loop].name)
														.attr(
																"id",
																arr[loop].pk_bankdoc);

												cate.append(objTrigger).append(
														isType).append(
														inputRadio).append(
														labelBank);

												treeItems.append(cate);
											}
											treeItems
													.insertAfter(trigger
															.parent(".bank_tree_category"));
										}
									}
								});

						// չ������
						trigger.parent(".bank_tree_category").next(
								".bank_tree_items").slideDown();
						// �����ֵܽڵ㼰���ӽڵ�
						trigger
								.parent(".bank_tree_category")
								.siblings(".bank_tree_category")
								.next(".bank_tree_items")
								.slideUp(
										function() {
											$(this)
													.prev(".bank_tree_category")
													.find(
															".bank_category_trigger")
													.removeClass("close");
										});

						return false;
					});

		$("#filterBank")
			.live(
					'click',
					function() {
						//$("#bankPicker").slideUp();
						//var bankDocObj = $(this).siblings("[name='bankDocName']");
						//currentBankObj = $(this).siblings("[name='bankDocName']");
						
						//var bankTypeIdObj = $(bankDocObj.parents("tr").find("[name='bankTypeId']")[0]);
						var bankTypeId = bankTypeIdObj.val();
						var bankTypeName = bankTypeIdObj.find("option:selected").text();
						
						$("#bankPicker").find(".bank_tree_list").empty();
						var div = $("<div></div>").addClass("bank_tree_category");
						var a = $("<a></a>").addClass("bank_category_trigger").attr("margin", 0);
						var isType = $("<input></input>").attr("name", "istype").attr("type",
												"hidden").val("Y");
						var label = $("<label></label>").attr("id",bankTypeId).text(bankTypeName);
						div.append(a).append(isType).append(label);
						$("#bankPicker").find(".bank_tree_list").append(div);
	
						//var position = $(this).parents("tr").find("[name='bankDocName']").offset();
						//position.top += 20;
						$("#bankPicker").slideDown();
						//$("#bankPicker").offset(position);
					});
					
	$("#bankPicked").live('click', function() {
		var items = $("#bankPicker").find("[name='bankid']");
		var bankName = "";
		var bankId = "";
		if (items != null && items.length > 0) {
			for ( var loop = 0; loop < items.length; loop++) {
				if (items[loop].checked == true) {
					bankName = $(items[loop]).next("label").text();
					bankId = $(items[loop]).attr("id");
					break;
				}
			}
			currentBankObj.val(bankName);
			currentBankObj.next().next().val(bankId);
			if(bankName != ""){
				currentBankObj.UITipInside();
			}
		}

		$("#bankPicker").slideUp();

		return false;
	});
	$("#bankCancel").live('click', function() {
		$("#bankPicker").slideUp();
		return false;
	});

	this.init = function() {

		$("body").delegate("[name='bankTypeId']", "change", function(){
			$($(this).parents("tr").find("[name='bankDocName']")[0]).val("");
			$($(this).parents("tr").find("[name='bankDocName']")[0]).change();
			$($(this).parents("tr").find("[name='pkBankDoc']")[0]).val("");
		});
		
		$("body").delegate(
				"[name='refbtn']",
				"click",
				function() {
					var bankDocObj = $(this).siblings("[name='bankDocName']");
					currentBankObj = $(this).siblings("[name='bankDocName']");
					
					bankTypeIdObj = $(bankDocObj.parents("tr").find("[name='bankTypeId']")[0]);
					var bankTypeId = bankTypeIdObj.val();
					var bankTypeName = bankTypeIdObj.find("option:selected").text();
					
					$("#bankPicker").find(".bank_tree_list").empty();
					var div = $("<div></div>").addClass("bank_tree_category");
					var a = $("<a></a>").addClass("bank_category_trigger").attr("margin", 0);
					var isType = $("<input></input>").attr("name", "istype").attr("type",
											"hidden").val("Y");
					var label = $("<label></label>").attr("id",bankTypeId).text(bankTypeName);
					div.append(a).append(isType).append(label);
					$("#bankPicker").find(".bank_tree_list").append(div);

					var position = $(this).parents("tr").find("[name='bankDocName']").offset();
					position.top += 20;
					$("#bankPicker").slideDown();
					$("#bankPicker").offset(position);
		});
	}
}

$(function() {
	var bank = new Bank();
	bank.init();
});
